package com.etung.iot.demo.storage;

import com.etung.iot.demo.handler.DefaultTopicHandler;
import com.etung.iot.demo.handler.Handler;
import com.etung.iot.demo.handler.TopicHandler;
import com.etung.iot.demo.modles.RealData;
import com.etung.iot.demo.utils.ClassUtil;
import com.etung.iot.demo.utils.SpringContextUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

@Slf4j
public class StorageFactory {
    private static Map<Integer, StorageHandler> handlerMaps = Maps.newHashMap();
    public static StorageHandler getStorageHandler(int type){
        return handlerMaps.getOrDefault(type, SpringContextUtils.getBean(RealStoreRule.class));
    }


    public static void registerHandler(Integer key,StorageHandler handler){
        handlerMaps.put(key,handler);
    }
    private void registerAllHandler() {

        try {
            for (Class cls: ClassUtil.getAllAssignedClass(StorageHandler.class)) {
                Storage annotation = (Storage)cls.getAnnotation(Storage.class);
                if(annotation != null) {
                    StorageHandler storageHandler = (StorageHandler) cls.getConstructor().newInstance();
                    handlerMaps.put(annotation.value(), storageHandler);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
